package com.itheima.controllor;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.MenuService;
import service.PermissionService;
import service.RoleService;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 包名:  com.iteitan.controller;
 * 作者:  Eitan
 * 日期:  2019/9/9 10:09
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @Reference
    private MenuService menuService;

    @Reference
    private PermissionService permissionService;

    /**
     * 添加角色以及设置权限
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        try {
            roleService.add(map);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 角色分页
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= roleService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e){
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 查询所有权限
     * @return
     */
    @RequestMapping("/findPermissionAll")
    public Result findPermissionAll(){
        try {
            List<Permission> permissionList = roleService.findPermissionAll();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
        } catch (Exception e){
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 根据角色id查找拥有的角色权限数据
     * @param roleId
     * @return
     */
    @RequestMapping("/findPermissionByRoleId")
    public Result findPermissionByRoleId(String roleId){
        try {
            Set<Permission> setList = roleService.findPermissionByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,setList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    /**
     * 根据角色id查找角色数据
     * @param id
     * @return
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        try {
            Role role = roleService.findRoleById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 根据角色id查找菜单数据
     * @param id
     * @return
     */
    @RequestMapping("/findMenuByRoleId")
    public List<Integer> findMenuByRoleId(Integer id){

        try {
            List<Integer> menuList=menuService.findMenuByRoleId(id);
            return menuList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 根据角色id查找菜单数据
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public List<Integer> findPermissionIdsByRoleId(Integer id){

        try {
            List<Integer> permissionIds=permissionService.findPermissionIdsByRoleId(id);
            return permissionIds;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 编辑角色以及设置权限
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map){
        try {
            roleService.edit(map);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    /**
     * 查询所有角色
     * @param
     * @return
     */
    @RequestMapping("/findRoleAll")
    public Result findRoleAll(){
        try {
            List<Role> roleList = roleService.findRoleAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

}
